﻿// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.


using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace HECore.IdentityServer4.Client_Credentials
{
    public class Startup
    {
        public IWebHostEnvironment Environment { get; }

        public Startup(IWebHostEnvironment environment)
        {
            Environment = environment;
        }

        public void ConfigureServices(IServiceCollection services)
        {
            // uncomment, if you want to add an MVC-based UI
            //services.AddControllersWithViews();

            /// 第三步：配置身份服务器
            /// 现在应该配置您的身份服务器。如果您运行服务器并将浏览器导航到
            /// 如果您运行服务器并将浏览器导航到
            /// https://localhost:5001/.well-known/openid-configuration
            /// 您应该会看到所谓的发现文档。发现文档是身份服务器中的标准端点。您的客户端和 API 将使用发现文档来下载必要的配置数据
            var builder = services.AddIdentityServer()
                .AddInMemoryApiScopes(Config.ApiScopes)
                .AddInMemoryClients(Config.Clients);

            //This is for dev only scenarios when you don’t have a certificate to use.
            //这适用于没有证书可供使用的仅用于开发的场景。
            //首次启动时，IdentityServer 将为您创建一个开发人员签名密钥
            //它是一个名为tempkey.jwk.您不必将该文件签入源代码管理中，如果该文件不存在，则会重新创建该文件。
            builder.AddDeveloperSigningCredential();
        }

        public void Configure(IApplicationBuilder app)
        {
            if (Environment.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            // uncomment if you want to add MVC
            //app.UseStaticFiles();
            //app.UseRouting();

            app.UseIdentityServer();

            // uncomment, if you want to add MVC
            //app.UseAuthorization();
            //app.UseEndpoints(endpoints =>
            //{
            //    endpoints.MapDefaultControllerRoute();
            //});
        }
    }
}
